<template>
  <div class="region">
    <div class="content">
      <div class="left">地区:</div>
      <ul>
        <li :class="activeFlag == '' ? 'active' : ''" @click="changeRegion('')">
          全部
        </li>
        <li
          v-for="item in regionArr"
          :key="item.id"
          @click="changeRegion(item.value)"
          :class="activeFlag == item.value ? 'active' : ''"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reqHospitalLevelAndRegion } from "@/api/home";
import {
  HospitalLevelAndRegionResponseData,
  HospitalLevelAndRegionArr,
} from "@/api/home/type.ts";
import { onMounted, ref, defineEmits } from "vue";

const $emit = defineEmits(["getRegion"]);

const regionArr = ref<HospitalLevelAndRegionArr>([]);
const getRegion = async () => {
  let data: HospitalLevelAndRegionResponseData =
    await reqHospitalLevelAndRegion("Beijin");
  regionArr.value = data.data;
};

const activeFlag = ref<string>("");
const changeRegion = (regionValue: string): void => {
  $emit("getRegion", regionValue);
  activeFlag.value = regionValue;
};

onMounted(() => {
  getRegion();
});
</script>

<style scoped lang="scss">
.region {
  color: #7f7f7f;
  margin-top: 10px;

  .content {
    margin-top: 20px;
    display: flex;

    .left {
      margin-right: 10px;
      width: 40px;
    }

    ul {
      flex: 1;
      display: flex;
      flex-wrap: wrap;

      li {
        margin-right: 10px;
        margin-bottom: 10px;

        &.active {
          color: #165dff;
        }

        &:hover {
          color: #165dff;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
